package com.example.demo.controller;

import com.example.demo.log.Log;
import com.example.demo.log.LogModuleEnum;
import com.example.demo.model.core.Add;
import com.example.demo.model.core.PageVo;
import com.example.demo.model.core.R;
import com.example.demo.model.core.Update;
import com.example.demo.model.form.PasswordForm;
import com.example.demo.model.form.UserForm;
import com.example.demo.model.form.UserPasswordForm;
import com.example.demo.model.form.UserProfileForm;
import com.example.demo.model.query.UserQuery;
import com.example.demo.model.vo.LoginUserVO;
import com.example.demo.model.vo.UserProfileVO;
import com.example.demo.model.vo.UserVO;
import com.example.demo.security.util.SecurityUtils;
import com.example.demo.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
@Tag(name = "用户管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('sys:user:add')")
    @Operation(summary = "添加用户")
    @Log(value = "添加用户", module = LogModuleEnum.USER)
    public R<Void> add(@Validated(Add.class) @RequestBody UserForm form) {
        sysUserService.add(form);
        return R.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('sys:user:edit')")
    @Operation(summary = "修改用户")
    @Log(value = "修改用户", module = LogModuleEnum.USER)
    public R<Void> update(@Validated(Update.class) @RequestBody UserForm form) {
        sysUserService.update(form);
        return R.ok();
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPerm('sys:user:delete')")
    @Operation(summary = "删除用户")
    @Log(value = "删除用户", module = LogModuleEnum.USER)
    public R<Void> delete(@RequestBody List<String> ids) {
        sysUserService.delete(ids);
        return R.ok();
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('sys:user:query')")
    @Operation(summary = "分页查询用户")
    public R<PageVo<UserVO>> page(@Validated UserQuery query) {
        PageVo<UserVO> page = sysUserService.page(query);
        return R.ok(page);
    }

    @GetMapping("/form")
    @Operation(summary = "用户表单")
    public R<UserForm> form(String id) {
        UserForm form = sysUserService.form(id);
        return R.ok(form);
    }

    @PostMapping("/me")
    @Operation(summary = "获取当前登录用户信息")
    public R<LoginUserVO> me() {
        LoginUserVO loginUserVo = sysUserService.getLoginUser();
        return R.ok(loginUserVo);
    }


    @GetMapping("/getProfile")
    @Operation(summary = "获取当前登录用户信息")
    public R<UserProfileVO> getUserProfile() {
        String userId = SecurityUtils.getUserId();
        UserProfileVO userProfile = sysUserService.getUserProfile(userId);
        return R.ok(userProfile);
    }

    @PostMapping("/updateProfile")
    @Operation(summary = "修改当前登录用户信息")
    @Log(value = "修改当前登录用户信息", module = LogModuleEnum.USER)
    public R<Void> updateUserProfile(@RequestBody UserProfileForm form) {
        String userId = SecurityUtils.getUserId();
        sysUserService.updateUserProfile(userId, form);
        return R.ok();
    }

    @PostMapping("/password")
    @Operation(summary = "修改当前登录用户密码")
    public R<Void> updatePassword(@Validated @RequestBody PasswordForm form) {
        String userId = SecurityUtils.getUserId();
        sysUserService.updateUserPassword(userId, form);
        return R.ok();
    }

    @PostMapping("/resetPassword")
    @Operation(summary = "重置用户密码")
    public R<Void> resetPassword(@Validated @RequestBody UserPasswordForm form) {
        sysUserService.updateUserPassword(form);
        return R.ok();
    }

}
